package org.starapp.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
	public static String getCurrentDateAndTime() {
		String currentDate = "";
		SimpleDateFormat format= new SimpleDateFormat("yyyy'-'MM'-'dd HH:mm:ss");
		format.setLenient(false);
		currentDate = format.format(new Date());
		return currentDate;
	}

	public static String getCurrentDate() {
		String currentDate = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyy'-'MM'-'dd");
		format.setLenient(false);
		currentDate = format.format(new Date());
		return currentDate;
	}

	public static String getCurDate() {
		String currentDate = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		format.setLenient(false);
		currentDate = format.format(new Date());
		return currentDate;
	}
    
	public static synchronized String getCurTime() {
		String pattern = "HHmm";
		return getDateFormat(getCalendar(), pattern);
	}
	
	public static synchronized String getCurrentTime() {
		String pattern = "HH:mm:ss";
		return getDateFormat(getCalendar(), pattern);
	}

	public static synchronized String getDateFormat(java.util.Calendar cal) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		return getDateFormat(cal, pattern);
	}

	public static synchronized String getDateFormat(java.util.Date date) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		return getDateFormat(date, pattern);
	}

	public static synchronized Calendar parseCalendarFormat(String strDate) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		return parseCalendarFormat(strDate, pattern);
	}


	public static synchronized Date parseDateFormat(String strDate) {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		return parseDateFormat(strDate, pattern);
	}

	public static synchronized String getDateFormat(java.util.Calendar cal,
			String pattern) {
		return getDateFormat(cal.getTime(), pattern);
	}

	public static synchronized String getCurrentTime(String pattern) {
		return getDateFormat(getCalendar(), pattern);
	}

	public static synchronized String getDateFormat(java.util.Date date,
			String pattern) {
		synchronized (sdf) {
			String str = null;
			sdf.applyPattern(pattern);
			str = sdf.format(date);
			return str;
		}
	}
	public static String getFormatDate(String time) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date;
		String strDate = "";
		try {
			date = df.parse(time);
			df.applyPattern("yyyy-MM-dd");
			strDate = df.format(date);
		} catch (ParseException e) {
		}
		return strDate;
	}


	public static String getPriorDay(int offset, String splitdate,
			String splittime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		Calendar theday = Calendar.getInstance();
		theday.add(Calendar.DATE, offset);

		df.applyPattern("yyyy" + splitdate + "MM" + splitdate + "dd" + " "
				+ "HH" + splittime + "mm" + splittime + "ss");

		return df.format(theday.getTime());
	}

	public static synchronized Date parseDateDayFormat(String strDate) {
		String pattern = "yyyy-MM-dd";
		return parseDateFormat(strDate, pattern);
	}
	private static SimpleDateFormat sdf = new SimpleDateFormat();

	public static synchronized Date parseDateFormat(String strDate,
			String pattern) {
		synchronized (sdf) {
			Date date = null;
			sdf.applyPattern(pattern);
			try {
				date = sdf.parse(strDate);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return date;
		}
	}
	public static synchronized Calendar getCalendar() {
		return GregorianCalendar.getInstance();
	}

	public static synchronized Calendar parseCalendarFormat(String strDate,
			String pattern) {
		synchronized (sdf) {
			Calendar cal = null;
			sdf.applyPattern(pattern);
			try {
				sdf.parse(strDate);
				cal = sdf.getCalendar();
			} catch (Exception e) {
			}
			return cal;
		}
	}
	public static void main(String args[]){
		System.out.println(formatChinaYear("2008"));
	}
	public static String formatChinaYear(String strYear)
	{
	      StringBuffer result=new StringBuffer();
	      String []en={"0","1","2","3","4","5","6","7","8","9"};
	      String []cn={"O","一","二","三","四","五","六","七","八","九"};
	      if(strYear.length()==0)
	      {
	    	  throw new RuntimeException("格式化的年度不能为空！");
	      }
	      if(!NumberUtils.isNumber(strYear))
	      {
	    	  throw new RuntimeException("年度中有非数字！");
	      }
	      for(int i=0;i<strYear.length();i++)
	      {
	    	  String temp=strYear.substring(i,i+1);
	    	  for(int j=0;j<en.length;j++)
	    	  {
	    		if(temp.equals(en[j])){
	    			result.append(cn[j]);
	    			break;
	    		}  
	    	  }
	      }
	      return result.toString();
	}
	public static String formatChinaMonth(String strMonth)
	{
	      StringBuffer result=new StringBuffer();
	      String []en={"0","1","2","3","4","5","6","7","8","9"};
	      String []cn={"O","一","二","三","四","五","六","七","八","九"};
	      if(strMonth.length()==0)
	      {
	    	  throw new RuntimeException("格式化的年度不能为空");
	      }
	      if(!NumberUtils.isNumber(strMonth))
	      {
	    	  throw new RuntimeException("月份中有非数字！");
	      }
	      for(int i=0;i<strMonth.length();i++)
	      {
	    	  String temp=strMonth.substring(i,i+1);
	    	  for(int j=0;j<en.length;j++)
	    	  {
	    		if(temp.equals(en[j])){
	    			result.append(cn[j]);
	    			break;
	    		}  
	    	  }
	      }
	      return result.toString();
	}
	public static String formatChinaDay(String strDay)
	{
	      StringBuffer result=new StringBuffer();
	      String []en={"0","1","2","3","4","5","6","7","8","9"};
	      String []cn={"O","一","二","三","四","五","六","七","八","九"};
	      if(strDay.length()==0)
	      {
	    	  throw new RuntimeException("格式化的年度不能为空");
	      }
	      if(!NumberUtils.isNumber(strDay))
	      {
	    	  throw new RuntimeException("日期中有非数字！");
	      }
	      for(int i=0;i<strDay.length();i++)
	      {
	    	  String temp=strDay.substring(i,i+1);
	    	  for(int j=0;j<en.length;j++)
	    	  {
	    		if(temp.equals(en[j])){
	    			result.append(cn[j]);
	    			break;
	    		}  
	    	  }
	      }
	      return result.toString();
	}
}
